package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.beans.MEventBeans;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonLogin;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.lc.rongzecaifu.view.ClearEditText;
import com.lc.rongzecaifu.view.PhoneFormatCheckUtils;
import com.zcx.helper.http.AsyCallBack;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    public final static String FLAG = "flag";
    @Bind(R.id.bt_registered)
    TextView btRegistered;
    @Bind(R.id.tv_forgotpw)
    TextView tvForgotpw;
    @Bind(R.id.et_account)
    ClearEditText etAccount;
    @Bind(R.id.et_password)
    ClearEditText etPassword;
    @Bind(R.id.bt_login)
    Button btLogin;
    @Bind(R.id.bt_register_repass)
    TextView bt_register_repass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        btRegistered.setOnClickListener(this);
        tvForgotpw.setOnClickListener(this);
        btLogin.setOnClickListener(this);
        bt_register_repass.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.bt_registered:
                Intent intent = new Intent();
                intent.putExtra(FLAG, 1);//注册模式
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_forgotpw:
                Intent intent1 = new Intent();
                intent1.putExtra(FLAG, 2);//忘记密码模式
                intent1.setClass(this, RegisterActivity.class);
                startActivity(intent1);
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.bt_login:
                onLogin();
                break;

            case R.id.bt_register_repass:
                Intent intent2 = new Intent();
                intent2.putExtra(FLAG, 2);//忘记密码模式
                intent2.setClass(this, ResetJiaoyiPwdActivity.class);
                startActivity(intent2);
                break;


        }
    }

    public void onLogin() {
        String string = etAccount.getText().toString().trim();
        PhoneFormatCheckUtils phoneFormatCheckUtils = new PhoneFormatCheckUtils();

        if (TextUtils.isEmpty(etAccount.getText().toString().trim())) {

            LovelyToast.makeText(LoginActivity.this, "手机号不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (etAccount.getText().toString().trim().length() < 6 || etAccount.getText().toString().trim().length() > 16) {

            LovelyToast.makeText(LoginActivity.this, "请输入正确的手机号码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!phoneFormatCheckUtils.isPhoneLegal(etAccount.getText().toString().trim())) {
            LovelyToast.makeText(LoginActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (etPassword.getText().toString().trim().isEmpty()) {
            LovelyToast.makeText(LoginActivity.this, "密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (etPassword.getText().toString().length() < 6 || etPassword.getText().toString().length() > 20) {


            LovelyToast.makeText(LoginActivity.this, "密码在6-15位", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {
            getAccessToken();
        }

    }

    private void initLogin(String access_token) {
        new JsonLogin(access_token, etAccount.getText().toString(), etPassword.getText().toString(), new AsyCallBack<JsonLogin.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonLogin.Info info) throws Exception {
                if (info != null) {

                    MEventBeans mEventBeans = new MEventBeans();
                    mEventBeans.type = 112;
                    EventBus.getDefault().post(mEventBeans);

                    MyApplication.myPreferences.saveUid(info.uid);

                    MyApplication.myPreferences.saveUserName(info.user_name);

                    MyApplication.myPreferences.saveUser_phone(etAccount.getText().toString().trim());

                    Log.e("DDDDDDDDDDD", etAccount.getText().toString().trim());


                    finish();

                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                LovelyToast.makeText(LoginActivity.this, JsonLogin.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }

    private void getAccessToken() {
        new JsonAccessToken("common/login", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initLogin(info.access_token);
                    } else {
                        //   UtilToast.show(LoginActivity.this, "安全验证失败！");
                        LovelyToast.makeText(LoginActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);

                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(LoginActivity.this, "安全验证失败！");
                LovelyToast.makeText(LoginActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }
}
